package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xq.shop.dao.domain.entity.GoodsType;
import com.xq.shop.dao.domain.entity.Menu;
import com.xq.shop.dao.mapper.MenuMapper;
import com.xq.shop.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.utils.MyConstants;
import com.xq.shop.vo.MenuVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-16
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public List<MenuVo> getMenuTree() {
        LambdaQueryWrapper<Menu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Menu::getStatus, DataStatus.NORMAL.getCode());
        List<Menu> menuList = list(wrapper);
        List<MenuVo> voList = beanToVo(menuList);
        return MyConstants.buildMenuTree(voList);
    }

    private List<MenuVo> beanToVo(List<Menu> menuList) {
        List<MenuVo> voList = new ArrayList<>();
        menuList.forEach(m -> {
            MenuVo vo = new MenuVo();
            BeanUtils.copyProperties(m, vo);
            voList.add(vo);
        });
        return voList;
    }


}
